/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.ScoreRule;
import com.boxin.ims.modules.wechat.service.ScoreRuleService;

/**
 * 积分规则Controller
 * @author haoziyang
 * @version 2014-12-30
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/scoreRule")
public class ScoreRuleController extends BaseController {

	@Autowired
	private ScoreRuleService scoreRuleService;
	
	@ModelAttribute
	public ScoreRule get(@RequestParam(required=false) Long id) {
		if (id != null){
			return scoreRuleService.get(id);
		}else{
			return new ScoreRule();
		}
	}

	@RequiresPermissions("wechat:scoreRule:view")
	@RequestMapping(value = {"list", ""})
	public String list(ScoreRule scoreRule, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			scoreRule.setUser(user);
		}
        Page<ScoreRule> page = scoreRuleService.find(new Page<ScoreRule>(request, response), scoreRule); 
        model.addAttribute("page", page);
		return "modules/wechat/scoreRuleList";
	}

	@RequiresPermissions("wechat:scoreRule:view")
	@RequestMapping(value = "form")
	public String form(ScoreRule scoreRule, Model model) {
	
		User user = UserUtils.getUser();
		if (scoreRule.getUser() == null || scoreRule.getUser().getId() <1){
			scoreRule.setUser(user);
		}
		
		model.addAttribute("scoreRule", scoreRule);
		return "modules/wechat/scoreRuleForm";
	}

	@RequiresPermissions("wechat:scoreRule:edit")
	@RequestMapping(value = "save")
	public String save(ScoreRule scoreRule, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, scoreRule)){
			return form(scoreRule, model);
		}
		scoreRuleService.save(scoreRule);
		addMessage(redirectAttributes, "保存积分规则'" + scoreRule.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/scoreRule/?repage";
	}
	
	@RequiresPermissions("wechat:scoreRule:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		scoreRuleService.delete(id);
		addMessage(redirectAttributes, "删除积分规则成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/scoreRule/?repage";
	}

}
